package com.itany.mall.dao.impl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.itany.mall.dao.UserDao;
import com.itany.mall.entity.User;
import com.itany.mall.exception.DataAccessException;
import com.itany.mall.factory.Component;

// 该类需要由工厂管理, 则加上工厂配套的注解
@Component
public class UserDaoImpl implements UserDao {

	@Override
	public User selectByLoginNameAndPassword(String name, String password) {
		Connection con = null;
		PreparedStatement st = null; // 预编译状态集
		ResultSet res = null;
		try {

			Class.forName("com.mysql.jdbc.Driver");
			StringBuffer url = new StringBuffer();
			url.append("jdbc:mysql://localhost:3306/mall");
			url.append("?useUnicode=true");
			url.append("&characterEncoding=utf8");
			url.append("&serverTimezone=Asia/Shanghai");
			con = DriverManager.getConnection(url.toString(), "root", "");

			StringBuffer sql = new StringBuffer();
			sql.append(" select ");
			sql.append("     u.id, u.name, u.password, ");
			sql.append("     u.phone, u.email, u.role ");
			sql.append(" from t_user u ");
			sql.append(" where ");
			sql.append("     (u.name = ? or u.phone = ? or u.email = ?) ");
			sql.append("     and u.password = ? ");

			st = con.prepareStatement(sql.toString());

			// setString不是指语句中是字符串
			// 而是指传递的name参数是String类型
			st.setString(1, name); // JDBC中的索引从1开始
			st.setObject(2, name); // 无法确定参数类型时, 可以直接setObject
			st.setObject(3, name);
			st.setObject(4, password);

			System.out.println(sql.toString());
			
			res = st.executeQuery();

			if (res.next()) {
				User u = new User();
				u.setId(res.getLong("id"));
				u.setName(res.getString("name"));
				u.setPassword(res.getString("password"));
				u.setPhone(res.getString("phone"));
				u.setEmail(res.getString("email"));
				u.setRole(res.getInt("role"));
				System.out.println(u);
				return u;
			}

			return null;

		} catch (Exception e) {
			e.printStackTrace();
			throw new DataAccessException();
		} finally {
			try {
				if (res != null)
					res.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				try {
					if (st != null)
						st.close();
				} catch (SQLException e) {
					e.printStackTrace();
				} finally {
					try {
						if (con != null)
							con.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}

}

//File dir = new File(Database02.USERS_DIR);
//File[] fs = dir.listFiles(new FileFilter() {
//	@Override
//	public boolean accept(File pathname) {
//		return pathname.getName().endsWith(Database02.USER_SUFFIX);
//	}
//});
//try {
//	for (File f : fs) {
//		ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f));
//		User u = (User) ois.readObject();
//		ois.close();
//		if ((u.getName().equals(name) || u.getPhone().equals(name)
//				|| u.getEmail().equals(name)) && u.getPassword().equals(password)) {
//			return u;
//		}
//	}
//} catch (Exception e) {
//	e.printStackTrace();
//}
//return null;
